package jpm.trade;

import static jpm.common.AssertUtils.ensure;
import static jpm.common.AssertUtils.ensureNotNull;
import jpm.common.JPMException;
import jpm.common.JPMException.ErrorCode;

public class TradeHelper {
	public enum Operation {
		NEW, AMEND, CANCEL
	}

	public static void ensureValid(Trade trade) {
		ensureNotNull(trade, "trade is null");
		ensure(trade.getTradeId() != 0, "tradeId is 0");
		ensure(trade.getTradeVersion() != 0, "tradeVersion is 0");
		ensureNotNull(trade.getSecurity(), "security is null");
		ensureNotNull(trade.getAccountNo(), "accountNo is null");
		ensureNotNull(trade.getOperation(), "operation is null");
	}

	public static void ensureSameTradeDetails(Trade trade1,
			Trade trade2) {
		ensureValid(trade1);
		ensureValid(trade2);

		// trade id
		int tradeId1 = trade1.getTradeId();
		int tradeId2 = trade2.getTradeId();
		if (tradeId1 != tradeId2)
			throw new JPMException().errorCode(ErrorCode.ERROR_INVALID_INPUT)
					.message(
							"Trade ids [" + tradeId1 + "] and [" + tradeId2
									+ "] do not match");

		int version1 = trade1.getTradeVersion();
		int version2 = trade2.getTradeVersion();

		// security
		String security1 = trade1.getSecurity();
		String security2 = trade2.getSecurity();
		if (!security1.equals(security2))
			throw new JPMException().errorCode(ErrorCode.ERROR_INVALID_INPUT)
					.message(
							"Security mismatch for cancel trade. Trade id [" + tradeId1
									+ "] version [" + version1
									+ "] has security [" + security1
									+ "] but version [" + version2
									+ "] has security [" + security2 + "]");

		// account no
		String account1 = trade1.getAccountNo();
		String account2 = trade2.getAccountNo();
		if (!account1.equals(account2))
			throw new JPMException().errorCode(ErrorCode.ERROR_INVALID_INPUT)
					.message(
							"Account mismatch for cancel trade. Trade id [" + tradeId1
									+ "] version [" + version1
									+ "] has account [" + account1
									+ "] but version [" + version2
									+ "] has account [" + account2 + "]");
	}
}
